/*
 * 文件名: YearMonthDayAmPmDialog.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XUJIAO001
 * 创建时间: 2011-12-28
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget.calendar.dialog;

import java.util.Calendar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import app.log.APPLog;
import app.utils.CommonUtils;
import app.widget.BaseDialog;
import app.widget.calendar.widget.OnWheelChangedListener;
import app.widget.calendar.widget.OnWheelScrollListener;
import app.widget.calendar.widget.WheelView;
import app.widget.calendar.widget.adapter.DateArrayAdapter;

import com.longevityassistant.activity.R;

/**
 * @author EX-XUJIAO001
 * @date 2011-12-28
 * @version [Android PABank C01, @2011-12-28]
 * @description 每周每月每天 日历 Dialog
 */
public class PerWeekPerMonthPerDayDialog extends BaseDialog implements
		OnWheelChangedListener, OnWheelScrollListener {
	/**
	 * 天
	 */
	private String str_day;
	/**
	 * 上午 下午
	 */
	private String str_ampm;
	/**
	 * 周
	 */
	private String str_week;
	/**
	 * 結果
	 */
	private String result;
	/**
	 * 當前月
	 */
	int curMonth;
	/**
	 * 当前周
	 */
	int curWeek;
	/**
	 * 当前天
	 */
	int curdays;
	/**
	 * 当前上午下午
	 */
	int cur_ampm;
	/**
	 * 当前选择
	 */
	private int curSelect;
	/**
	 * 上下文
	 */
	private Context context;
	/**
	 * 每的view
	 */
	WheelView per;
	/**
	 * 月的view
	 */
	WheelView month;
	/**
	 * 周的view
	 */
	WheelView week;
	/**
	 * 天的view
	 */
	WheelView day;
	/**
	 * 上午下午的view
	 */
	WheelView ampm;
	/**
	 * 空白的view
	 */
	WheelView block;

	/**
	 * 当前 per
	 */
	private String curper;
	/**
	 * 返回的View
	 */
	private View contentView;
	/**
	 * 获取后台时间
	 */
	private Calendar calendar;

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 * @param title
	 *            标题
	 */
	public PerWeekPerMonthPerDayDialog(Context context, String title) {

		super(context);
		this.context = context;
		setTitle(title);
		initDate();
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化
	 */
	private void initDate() {
		initView();

		CalendarDialogCommon.addChangeListener(this, per, week, day, ampm);

		setBtn1ClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onDialogScrollEndListener.onDialogScrollEnd(result);
				PerWeekPerMonthPerDayDialog.this.dismiss();
			}
		});

		setBtn2ClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				PerWeekPerMonthPerDayDialog.this.dismiss();
			}
		});

		//Calendar calendar = Calendar.getInstance();
		calendar = CommonUtils.getCurrentTimeByCalendarAmpm();

		block.setViewAdapter(new DateArrayAdapter(context,
				CalendarDialogCommon.BLOCK, 0));

		// per

		per.setViewAdapter(new DateArrayAdapter(context,
				CalendarDialogCommon.PERS, 0));
		per.setCurrentItem(0);

		// per.addChangingListener(listener);
		// week

		curWeek = calendar.get(Calendar.DAY_OF_WEEK);
		APPLog.i("zbkc", "curWeek:" + curWeek);
		week.setViewAdapter(new DateArrayAdapter(context,
				CalendarDialogCommon.WEEKS, curWeek - 1));
		week.setCurrentItem(curWeek - 1);
		// week.addChangingListener(listener);

		// day
		CalendarDialogCommon.updateDays(context, day ,calendar);

		day.setCurrentItem(calendar.get(Calendar.DAY_OF_MONTH) - 1);
		// day.addChangingListener(listener);

		// amppm
		cur_ampm = calendar.get(Calendar.AM_PM);
		APPLog.i("zbkc", cur_ampm + "");
		ampm.setViewAdapter(new DateArrayAdapter(context,
				CalendarDialogCommon.AM_PM, cur_ampm));
		ampm.setCurrentItem(cur_ampm);
		// ampm.addChangingListener(listener);

		str_day = calendar.get(Calendar.DAY_OF_MONTH) + "";

		str_week = (curWeek - 1) + "";
		str_ampm = "0" + cur_ampm;
		if (curSelect == 0) {
			curper = 1 + "";
		} else if (curSelect == 1) {
			curper = 2 + "";
		} else if (curSelect == 2) {
			curper = 0 + "";
		}

		result = curper + "-" + str_week + "-" + str_ampm;
		APPLog.i("zbkc", result);

	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化View
	 */
	private void initView() {
		contentView = LayoutInflater.from(context).inflate(
				R.layout.app_widget_perweek_permonth_perday_layout, null);

		per = (WheelView) contentView.findViewById(R.id.per);
		month = (WheelView) contentView.findViewById(R.id.month);
		week = (WheelView) contentView.findViewById(R.id.week);
		day = (WheelView) contentView.findViewById(R.id.day);
		ampm = (WheelView) contentView.findViewById(R.id.ampm);
		block = (WheelView) contentView.findViewById(R.id.block);

	}

	/**
	 * 滚动停止监听器
	 */
	private OnDialogScrollEndListener onDialogScrollEndListener;

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @param onDialogScrollEndListener
	 *            滚动停止监听器
	 */
	public void setOnDialogScrollEndListener(
			OnDialogScrollEndListener onDialogScrollEndListener) {
		this.onDialogScrollEndListener = onDialogScrollEndListener;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @return OnDialogScrollEndListener
	 */
	public OnDialogScrollEndListener getOnDialogScrollEndListener() {
		return onDialogScrollEndListener;
	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		// TODO Auto-generated method stub

		switch (wheel.getId()) {
		case R.id.per:
			switch (newValue) {
			case 0:
				day.setVisibility(View.GONE);
				week.setVisibility(View.VISIBLE);
				block.setVisibility(View.GONE);
				curSelect = 0;
				break;
			case 1:
				day.setVisibility(View.VISIBLE);
				week.setVisibility(View.GONE);
				block.setVisibility(View.GONE);
				curSelect = 1;
				break;
			case 2:
				block.setVisibility(View.VISIBLE);
				day.setVisibility(View.GONE);
				week.setVisibility(View.GONE);
				curSelect = 2;
				break;
			default:
				break;
			}
			break;
		case R.id.week:
			str_week = newValue + "";
			break;

		case R.id.day:
			str_day = 1 + newValue + "";
			break;
		case R.id.ampm:
			str_ampm = "0" + newValue;
			break;
		default:
			break;
		}

		if (curSelect == 0) {
			curper = 1 + "";
		} else if (curSelect == 1) {
			curper = 2 + "";
		} else if (curSelect == 2) {
			curper = 0 + "";
		}

		switch (curSelect) {
		case 0:
			result = curper + "-" + str_week + "-" + str_ampm;
			break;
		case 1:
			result = curper + "-" + str_day + "-" + str_ampm;
			break;
		case 2:
			result = curper + "-" + str_ampm;
			break;
		default:
			break;
		}

		CalendarDialogCommon.updateDays(context, day ,calendar);

		APPLog.i("zbkc", result);

	}

	@Override
	public View createContentView() {
		// TODO Auto-generated method stub
		return contentView;
	}

	@Override
	public void onScrollingStarted(WheelView wheel) {
		setBtn1Enable(false);
		setBtn2Enable(false);

	}

	@Override
	public void onScrollingFinished(WheelView wheel) {
		setBtn1Enable(true);
		setBtn2Enable(true);
	}
}
